package com.bilibili.juc.completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author zuife
 * @description
 * @since 2024-04-20 16:48:35
 */
public class CompletableFutureFastDemo {
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(5);
        CompletableFuture<String> playA=CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName()+ "\t"+"A...开始");
            return "playA";
        });
        CompletableFuture<String> playB=CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(4);
                System.out.println(Thread.currentThread().getName()+ "\t"+"B...开始");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "playB";
        });
        CompletableFuture<String> result = playA.applyToEither(playB, (res) -> {
            return res + " is winner";
        });
        System.out.println(Thread.currentThread().getName()+"\t"+"----------"+result.join());
    }
}
